package com.zhu.gmallpublisher.controller;

import com.sun.jmx.snmp.mpm.SnmpMsgTranslator;
import com.zhu.gmallpublisher.bean.TradeProvinceOrderCt;
import com.zhu.gmallpublisher.service.TradeStatsService;
import com.zhu.gmallpublisher.utils.GetDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 交易域 控制层
 */

@RestController
@RequestMapping("/zhu/gmall/sugar/trade/")
public class TradeController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/provinceOrderCt")
    public String  getTradeProvinceOrderCt(@RequestParam(value = "date",defaultValue = "0") Integer selectDate){
        if(selectDate == null) {
            selectDate = GetDateUtil.getCurrentTime();
        }
        List<TradeProvinceOrderCt> tradeProvinceOrderCtList = tradeStatsService.getTradeProvinceOrderCt(selectDate);
        if(tradeProvinceOrderCtList == null || tradeProvinceOrderCtList.size() == 0){
            return "";

        }
        StringBuilder dataStringJson = new StringBuilder("[");
        for (int i = 0; i < tradeProvinceOrderCtList.size(); i++) {
            TradeProvinceOrderCt tradeProvinceOrderCt = tradeProvinceOrderCtList.get(i);
            String provinceName = tradeProvinceOrderCt.getProvinceName();
            String orderCt = tradeProvinceOrderCt.getOrderCt();
            dataStringJson
                    .append("{\n" + "        \"name\": \"").append(provinceName)
                    .append("\",\n").append("        \"value\": ")
                    .append(orderCt).append("\n").append("      }");

            if(i < tradeProvinceOrderCtList.size() - 1){
                dataStringJson.append(",");
            }else {
                dataStringJson.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": " + dataStringJson + ",\n" +
                "    \"valueName\": \"订单数\"\n" +
                "  }\n" +
                "}";

    }
}
